<?php
namespace src\logic;
use classlib\acl;
use classlib\input;
use \classlib\core as C;
class ladresult {
	const COOKIE_ADOS = 'ados';
	const COOKIE_LAST = 'adlt';
	const ADARGS_ADOS = 's';
	const ADDATE_TIME = '2016-07-17 20:00:00';//'2016-08-17 20:00:00';
	const ADTIME_ZONE = 'PST8PDT';
	const ADCHAM_SIGN = 'igg@pocketlive.tv';
	const ADLAST_TIME = 900;//15分钟
	
	public static function get_url() {
		$hash = substr(md5(self::ADCHAM_SIGN), 0, 8);
		echo C\router::get_url('home', '', array(self::ADARGS_ADOS=>$hash));
	}
	
	private static function __actions() {
		$acl  = \classlib\acl::inst('ACLweb');
		if (!$acl->islogin()) {
			return;//未登录的账号直接略过
		}
		$torm   = C\model::load('main.log.tbresult');
		$tbuser = $torm->get_one($acl->user['id']);
		if (!$tbuser) {
			return;//非绑定跟踪的账号直接跳过去了
		}
		//15分钟逗留的判断
		$data = input::cookie(self::COOKIE_LAST);
		if ($tbuser['duration'] < self::ADLAST_TIME && $data) {
			list($stime, $userid) = explode('-', $data);
			$duation = C\config::$nowtime - $stime;
			if ($tbuser['duration'] < $duation) {
				$field = array('duration'=>$duation, 'stime'=>C\config::$nowtime);
				$torm->save($acl->user['id'], $field);
			}
		}
		//互动资料信息
		$action = input::post('__action');
		$mapers = array('gift', 'share', 'im', 'prise');
		if ($action && in_array($action, $mapers)) {
			$actions   = empty($tbuser['actions'])? array():explode(',', $tbuser['actions']);
			if (!in_array($action, $actions)) {//事件未记录过的记录一下
				$actions[] = $action;
				$field = array('actions'=>implode(',', $actions), 'stime'=>config::$nowtime);
				$torm->save($acl->user['id'], $field);
			}
		}
		//如果跟踪代码不存在的话继续处理跟踪代码
		if (!input::cookie(self::COOKIE_ADOS)) {
			$hash = substr(md5(self::ADCHAM_SIGN), 0, 8);
			setcookie(self::COOKIE_ADOS, $hash, 0, '/');
		}
		if (!input::cookie(self::COOKIE_LAST)) {
			$data = sprintf('%d-%d', C\config::$nowtime, $acl->user['id']);
			setcookie(self::COOKIE_LAST, $data, 0, '/');
		}
	}
	
	//初始化设置cookie
	public static function __init__() {
		self::__actions();
		$admi = input::get(self::ADARGS_ADOS);
		$hash = substr(md5(self::ADCHAM_SIGN), 0, 8);
		if ($admi && strcmp($admi, $hash) != 0) {
			return; //设置了标记，但是不合法的话直接无视
		}
		$sign = input::cookie(self::COOKIE_ADOS);
		if (!$sign && $admi) {//记录跟踪代码
			 setcookie(self::COOKIE_ADOS, $admi, 0, '/');
			return;
		}
	}
	
	//放在会员注册的地方需要注意一下
	public static function __register($user) {
		$sign = input::cookie(self::COOKIE_ADOS);
		if (!$sign || !$user) {
			return;
		}
		$date = new DateTime(self::ADDATE_TIME, new DateTimeZone(self::ADTIME_ZONE));
		$unixtime = $date->getTimestamp();
		if ($user['regtime'] < $unixtime) {
			return;//账号是很早之前注册的直接返回
		}
		$torm   = C\model::load('main.log.tbresult');
		$field  = array('userid'=>$user['userid'], 'regtime'=>$user['regtime'],
			'origin'=>$user['origin'], 'ip'=>C\get_ip(), 'stime'=>config::$nowtime);
		$torm->new_one($field);
		//记录跟踪时间
		$data = sprintf('%d-%d', C\config::$nowtime, $user['userid']);
		setcookie(self::COOKIE_LAST, $data, 0, '/');
	}
	
	//放在会员邮箱绑定验证的地方
	public static function __verify($userid) {
		$torm   = C\model::load('main.log.tbresult');
		$res    = $torm->get_one($userid);
		if (!$res) {
			return; //识别码已经被绑定过了
		}
		$field = array('isverify'=>1, 'stime'=>config::$nowtime);
		$torm->save($userid, $field);
		if (!input::cookie(self::COOKIE_LAST)) {
			$data = sprintf('%d-%d', C\config::$nowtime, $userid);
			setcookie(self::COOKIE_LAST, $data, 0, '/');
		}
	}
}